package com.ykdl.member.kid.util;

import java.io.File;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Environment;
import android.widget.Toast;

/**
 * 获取 外部sd卡的路径
 * 
 * @see
 * @since 1.0
 */
public class SysUtil {

	public static String getExternalStoragePath() {

		// 获取SdCard状态

		String state = android.os.Environment.getExternalStorageState();

		// 判断SdCard是否存在并且是可用的

		if (android.os.Environment.MEDIA_MOUNTED.equals(state)) {
			if (android.os.Environment.getExternalStorageDirectory().canWrite()) {
				return android.os.Environment.getExternalStorageDirectory()
						.getPath();
			}
		}
		return null;
	}

	private static boolean checkFsWritable() {
		// Create a temporary file to see whether a volume is really writeable.
		// It's important not to put it in the root directory which may have a
		// limit on the number of files.
		String directoryName = Environment.getExternalStorageDirectory()
				.toString() + "/DCIM";
		File directory = new File(directoryName);
		if (!directory.isDirectory()) {
			if (!directory.mkdirs()) {
				return false;
			}
		}
		return directory.canWrite();
	}

	public static boolean hasStorage() {
		return hasStorage(true);
	}

	public static boolean hasStorage(boolean requireWriteAccess) {
		String state = Environment.getExternalStorageState();

		if (Environment.MEDIA_MOUNTED.equals(state)) {
			if (requireWriteAccess) {
				boolean writable = checkFsWritable();
				return writable;
			} else {
				return true;
			}
		} else if (!requireWriteAccess
				&& Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
			return true;
		}
		return false;
	}

	/**
	 * 是否取消订单
	 * 
	 * @param context
	 */
	public static void getWhetherCancleOrderDialog(final Context context) {
		Dialog dialog = new AlertDialog.Builder(context)
				.setMessage("您是否要取消订单，取消后订单款项会在两个工作日内原途经返回到您的账户上。")
				.setPositiveButton("是", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						Toast.makeText(context, "点击了是", 0).show();

					}
				})
				.setNegativeButton("否", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						Toast.makeText(context, "点击了否", 0).show();
						dialog.cancel();
					}
				}).create();
		dialog.show();
	}

	/**
	 * 取消订单
	 * 
	 * @param context
	 */
	public static void getCancleOrderDialog(final Context context) {
		Dialog dialog = new AlertDialog.Builder(context)
				.setMessage(
						"您的订单已取消，订单款项会在两个工作日内原途经返回到您的账户，请注意查收，如有问题请及时与我们的客服人员联系！")
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						Toast.makeText(context, "点击了确定", 0).show();
					}
				}).create();
		dialog.show();
	}

}
